package com.xin.minibusmall.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xin.minibusmall.product.entity.AttrEntity;
import com.xin.minibusmall.product.service.AttrAttrgroupRelationService;
import com.xin.minibusmall.product.service.AttrService;
import com.xin.minibusmall.product.service.CategoryService;
import com.xin.minibusmall.product.vo.AttrGroupWithAttrsResVo;
import com.xin.minibusmall.product.vo.AttrRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.xin.minibusmall.product.entity.AttrGroupEntity;
import com.xin.minibusmall.product.service.AttrGroupService;
import com.xin.common.utils.PageUtils;
import com.xin.common.utils.R;



/**
 * 属性分组
 *
 * @author fbb
 * @email 1339279838@qq.com
 * @date 2020-08-23 17:35:29
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    /**
     * 获取属性分组以及它的属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsResVo> vos = attrGroupService.getAttrGroupWithAttrByCatelogId(catelogId);

        return R.ok().put("data", vos);
    }
    /**
     * 新建关联
     */
    @PostMapping("/attr/relation")
    public R savaRelation(@RequestBody List<AttrRelationVo> vos){
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }
    /**
     * 删除关联信息
     * 前台传送数组，从而可以单个删除或者批量删除
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrRelationVo[] attrRelationVo){
        attrAttrgroupRelationService.deleteRelation(attrRelationVo);
        return R.ok();
    }
    /**
     * 查询属性分组没有关联的，从而能进行关联
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noAttrRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils pageUtils = attrService.getNoRelationAttr(attrgroupId, params);
        return R.ok().put("page", pageUtils);
    }

    /**
     * 查询属性分组的关联
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 对应cateId的list
     */
    @RequestMapping("/list/{catelogId}")
    public R CateList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long catelogId = attrGroup.getCatelogId();
        attrGroup.setCatelogPath(categoryService.findCatelogPath(catelogId));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeAttrGroups(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
